package com.ruofeng.app.kotlin.second.wechat.starter

import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.Test

/**
 * Created by 邱鹏 on 2017/8/23.
 */
class StaticClass {
    /**
     * 用object修饰，相当于Java中的static，用object修饰一个变量，可以实现全局变量的效果
     */
    object list {
        var memberList: ArrayList<Map<String, String>>? = ArrayList()
    }

    companion object {
        //用companion object包裹方法，实现java中static的效果
        var mapper = ObjectMapper()

        /**
         * 添加一个成员map到全局集合中
         */
        fun addMember(member: Map<String, String>) {
            list.memberList?.add(member)
        }

        /**
         *打印全局变量的所有内容
         */
        fun printMemberList() {
            println(mapper.writeValueAsString(list))
        }
    }
}

class TestStatic {
    @Test
    fun testAccessStatic() {
        var member = mapOf<String, String>("hello" to "world", "book" to "thinking in java")
        StaticClass.addMember(member)
        StaticClass.printMemberList()
    }
}